package com.day11.study.annotation_demo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class GetAnnotation {
    public static void main(String[] args) throws Exception {
        Student student = new Student();
        Class cls = student.getClass();
        boolean exists = cls.isAnnotationPresent(SelfAnnotation.class);
        if (exists) {
            SelfAnnotation annotation = (SelfAnnotation) cls.getAnnotation(SelfAnnotation.class);
            System.out.println(annotation.name());
            System.out.println(annotation.age());
            for (String str : annotation.values()) {
                System.out.println(str);
            }
            System.out.println("\n");
        }
        Method method = cls.getDeclaredMethod("getStuName", null);
        if (method.isAnnotationPresent(SelfAnnotation.class)) {
            SelfAnnotation annotation = (SelfAnnotation) method.getAnnotation(SelfAnnotation.class);
            System.out.println(annotation.name());
            System.out.println(annotation.age());
            for (String str : annotation.values()) {
                System.out.println(str);
            }
            System.out.println("\n");
        }
        Field f = cls.getDeclaredField("stuName");
        if (method.isAnnotationPresent(SelfAnnotation.class)) {
            SelfAnnotation annotation = (SelfAnnotation) f.getAnnotation(SelfAnnotation.class);
            System.out.println(annotation.name());
            System.out.println(annotation.age());
            for (String str : annotation.values()) {
                System.out.println(str);
            }
            System.out.println("\n");
        }

    }
}
